 package uk.co.mmscomputing.imageio.bmp;
 
 import java.awt.image.IndexColorModel;
 import javax.imageio.metadata.IIOMetadata;
 import javax.imageio.metadata.IIOMetadataNode;
 import org.w3c.dom.Node;
 
 public class BMPMetadata extends IIOMetadata
   implements BMPConstants
 {
   private static final String formatName = "javax_imageio_bmp_1.0";
   private int width;
   private int height;
   private int bitsPerPixel;
   private int compression;
   private int imageSize;
   private int xPixelsPerMeter;
   private int yPixelsPerMeter;
   private int colorsUsed;
   private int colorsImportant;
   private int redMask;
   private int greenMask;
   private int blueMask;
   private IndexColorModel icm;
 
   public BMPMetadata()
   {
     super(true, "javax_imageio_bmp_1.0", "uk.co.mmscomputing.imageio.bmp.BMPMetadata", null, null);
     this.xPixelsPerMeter = 2953;
     this.yPixelsPerMeter = 2953;
   }
   public void setWidth(int paramInt) {
     this.width = paramInt; } 
   public void setHeight(int paramInt) { this.height = paramInt; } 
   public void setBitsPerPixel(int paramInt) { this.bitsPerPixel = paramInt; } 
   public void setCompression(int paramInt) { this.compression = paramInt; } 
   public void setImageSize(int paramInt) { this.imageSize = paramInt; } 
   public void setXPixelsPerMeter(int paramInt) {
     this.xPixelsPerMeter = paramInt; } 
   public void setYPixelsPerMeter(int paramInt) { this.yPixelsPerMeter = paramInt; }
 
   public void setXDotsPerInch(int paramInt)
   {
     this.xPixelsPerMeter = ((int)Math.round(paramInt * 1000.0D / 25.399999999999999D)); } 
   public void setYDotsPerInch(int paramInt) { this.yPixelsPerMeter = ((int)Math.round(paramInt * 1000.0D / 25.399999999999999D)); } 
   public void setColorsUsed(int paramInt) {
     this.colorsUsed = paramInt; } 
   public void setColorsImportant(int paramInt) { this.colorsImportant = paramInt; } 
   public void setRedMask(int paramInt) { this.redMask = paramInt; } 
   public void setGreenMask(int paramInt) { this.greenMask = paramInt; } 
   public void setBlueMask(int paramInt) { this.blueMask = paramInt; }
 
   public void setIndexColorModel(IndexColorModel paramIndexColorModel) {
     if (paramIndexColorModel != null) this.colorsUsed = paramIndexColorModel.getMapSize();
     this.icm = paramIndexColorModel;
   }
   public int getXPixelsPerMeter() {
     return this.xPixelsPerMeter; } 
   public int getYPixelsPerMeter() { return this.yPixelsPerMeter; }
 
   public int getXDotsPerInch() {
     return (int)Math.round(this.xPixelsPerMeter * 25.399999999999999D / 1000.0D); } 
   public int getYDotsPerInch() { return (int)Math.round(this.yPixelsPerMeter * 25.399999999999999D / 1000.0D); } 
   public IndexColorModel getIndexColorModel() {
     return this.icm;
   }
   public boolean isReadOnly() { return false; }
 
   public Node getAsTree(String paramString) {
     if (paramString.equals("javax_imageio_bmp_1.0"))
       return getNativeTree();
     if (paramString.equals("javax_imageio_1.0")) {
       return getStandardTree();
     }
     throw new IllegalArgumentException(getClass().getName() + ".mergeTree:\n\tUnknown format: " + paramString);
   }
 
   public void mergeTree(String paramString, Node paramNode) {
     throw new IllegalStateException(getClass().getName() + ".mergeTree:\n\tFunction not supported.");
   }
 
   public void reset() {
     throw new IllegalStateException(getClass().getName() + ".reset:\n\tFunction not supported.");
   }
 
   private Node getNativeTree() {
     IIOMetadataNode localIIOMetadataNode1 = new IIOMetadataNode("javax_imageio_bmp_1.0");
     addChildNode(localIIOMetadataNode1, "BMPVersion", "BMP v. 3.x");
     addChildNode(localIIOMetadataNode1, "Width", new Integer(this.width));
     addChildNode(localIIOMetadataNode1, "Height", new Integer(this.height));
     addChildNode(localIIOMetadataNode1, "BitsPerPixel", new Integer(this.bitsPerPixel));
     addChildNode(localIIOMetadataNode1, "Compression", new Integer(this.compression));
     addChildNode(localIIOMetadataNode1, "ImageSize", new Integer(this.imageSize));
 
     IIOMetadataNode localIIOMetadataNode2 = addChildNode(localIIOMetadataNode1, "PixelsPerMeter", null);
     addChildNode(localIIOMetadataNode2, "X", new Integer(this.xPixelsPerMeter));
     addChildNode(localIIOMetadataNode2, "Y", new Integer(this.yPixelsPerMeter));
 
     addChildNode(localIIOMetadataNode1, "ColorsUsed", new Integer(this.colorsUsed));
     addChildNode(localIIOMetadataNode1, "ColorsImportant", new Integer(this.colorsImportant));
 
     if (this.icm != null) {
       localIIOMetadataNode2 = addChildNode(localIIOMetadataNode1, "Palette", null);
       for (int i = 0; i < this.colorsUsed; i++) {
         IIOMetadataNode localIIOMetadataNode3 = addChildNode(localIIOMetadataNode2, "PaletteEntry", null);
         addChildNode(localIIOMetadataNode3, "Red", new Byte((byte)this.icm.getRed(i)));
         addChildNode(localIIOMetadataNode3, "Green", new Byte((byte)this.icm.getGreen(i)));
         addChildNode(localIIOMetadataNode3, "Blue", new Byte((byte)this.icm.getBlue(i)));
       }
     }
     return localIIOMetadataNode1;
   }
 
   protected IIOMetadataNode getStandardChromaNode() {
     IIOMetadataNode localIIOMetadataNode1 = new IIOMetadataNode("Chroma");
     if (this.icm != null) {
       IIOMetadataNode localIIOMetadataNode2 = new IIOMetadataNode("Palette");
       localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
       for (int i = 0; i < this.colorsUsed; i++) {
         IIOMetadataNode localIIOMetadataNode3 = new IIOMetadataNode("PaletteEntry");
         localIIOMetadataNode2.appendChild(localIIOMetadataNode3);
         localIIOMetadataNode3.setAttribute("index", "" + i);
         localIIOMetadataNode3.setAttribute("red", "" + this.icm.getRed(i));
         localIIOMetadataNode3.setAttribute("green", "" + this.icm.getGreen(i));
         localIIOMetadataNode3.setAttribute("blue", "" + this.icm.getBlue(i));
         localIIOMetadataNode3.setAttribute("alpha", "255");
       }
     }
     return localIIOMetadataNode1;
   }
 
   protected IIOMetadataNode getStandardCompressionNode() {
     IIOMetadataNode localIIOMetadataNode1 = new IIOMetadataNode("Compression");
     IIOMetadataNode localIIOMetadataNode2 = new IIOMetadataNode("CompressionTypeName");
     localIIOMetadataNode2.setAttribute("value", BMPConstants.compressionTypeNames[this.compression]);
     localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
     return localIIOMetadataNode1;
   }
 
   private String countBits(int paramInt) {
     int i = 0;
     while (paramInt > 0) {
       if ((paramInt & 0x1) == 1) i++;
       paramInt >>>= 1;
     }
     return "" + i;
   }
 
   protected IIOMetadataNode getStandardDataNode() {
     IIOMetadataNode localIIOMetadataNode1 = new IIOMetadataNode("Data");
     String str = "";
     if (this.bitsPerPixel == 24)
       str = "8 8 8";
     else if ((this.bitsPerPixel == 16) || (this.bitsPerPixel == 32))
       str = str + countBits(this.redMask) + " " + countBits(this.greenMask) + " " + countBits(this.blueMask);
     else if (this.bitsPerPixel <= 8) {
       str = str + this.bitsPerPixel;
     }
     IIOMetadataNode localIIOMetadataNode2 = new IIOMetadataNode("BitsPerSample");
     localIIOMetadataNode2.setAttribute("value", str);
     localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
 
     return localIIOMetadataNode1;
   }
 
   protected IIOMetadataNode getStandardDimensionNode() {
     if ((this.yPixelsPerMeter > 0.0D) && (this.xPixelsPerMeter > 0.0D)) {
       IIOMetadataNode localIIOMetadataNode1 = new IIOMetadataNode("Dimension");
 
       double d = this.yPixelsPerMeter / this.xPixelsPerMeter;
       IIOMetadataNode localIIOMetadataNode2 = new IIOMetadataNode("PixelAspectRatio");
       localIIOMetadataNode2.setAttribute("value", "" + d);
       localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
 
       d = 1000.0D / this.xPixelsPerMeter;
       localIIOMetadataNode2 = new IIOMetadataNode("HorizontalPhysicalPixelSpacing");
       localIIOMetadataNode2.setAttribute("value", "" + d);
       localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
 
       d = 1000.0D / this.yPixelsPerMeter;
       localIIOMetadataNode2 = new IIOMetadataNode("VerticalPhysicalPixelSpacing");
       localIIOMetadataNode2.setAttribute("value", "" + d);
       localIIOMetadataNode1.appendChild(localIIOMetadataNode2);
 
       return localIIOMetadataNode1;
     }
     return null;
   }
 
   private String objectToString(Object paramObject) {
     return paramObject.toString();
   }
 
   private IIOMetadataNode addChildNode(IIOMetadataNode paramIIOMetadataNode, String paramString, Object paramObject) {
     IIOMetadataNode localIIOMetadataNode = new IIOMetadataNode(paramString);
     if (paramObject != null) {
       localIIOMetadataNode.setUserObject(paramObject);
       localIIOMetadataNode.setNodeValue(objectToString(paramObject));
     }
     paramIIOMetadataNode.appendChild(localIIOMetadataNode);
     return localIIOMetadataNode;
   }
 }

